package com.fzhucc.modules.security.impl;

import com.fzhucc.modules.security.domain.UserCode;
import com.fzhucc.modules.security.domain.dto.UserCodeDto;
import com.fzhucc.modules.security.domain.dto.UserCodeQueryCriteria;
import com.fzhucc.modules.security.mapstruct.UserCodeMapper;
import com.fzhucc.modules.security.repository.UserCodeRepository;
import com.fzhucc.modules.security.service.UserCodeService;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author yyg
* @date 2023-10-07
**/
@Service
@RequiredArgsConstructor
public class UserCodeServiceImpl implements UserCodeService {

    private final UserCodeRepository userCodeRepository;
    private final UserCodeMapper userCodeMapper;

    @Override
    public Map<String,Object> queryAll(UserCodeQueryCriteria criteria, Pageable pageable){
        Page<UserCode> page = userCodeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userCodeMapper::toDto));
    }

    @Override
    public List<UserCodeDto> queryAll(UserCodeQueryCriteria criteria){
        return userCodeMapper.toDto(userCodeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UserCodeDto findById(Integer id) {
        UserCode userCode = userCodeRepository.findById(id).orElseGet(UserCode::new);
        ValidationUtil.isNull(userCode.getId(),"UserCode","id",id);
        return userCodeMapper.toDto(userCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserCodeDto create(UserCode resources) {
        return userCodeMapper.toDto(userCodeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserCode resources) {
        UserCode userCode = userCodeRepository.findById(resources.getId()).orElseGet(UserCode::new);
        ValidationUtil.isNull( userCode.getId(),"UserCode","id",resources.getId());
        userCode.copy(resources);
        userCodeRepository.save(userCode);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            userCodeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UserCodeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserCodeDto userCode : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("username", userCode.getPhone());
            map.put("码校验用", userCode.getCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}